package cn.tnar.controller.aop;

import cn.tnar.constant.ResultCode;
import cn.tnar.model.entity.TAcctCustLoginInfo;
import cn.tnar.response.Result;
import cn.tnar.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 陈亮
 * @Date: 2018/10/11 22:51
 */

@Aspect
@Component
@Slf4j
public class TokenAop {

    @Pointcut("@annotation(cn.tnar.annotation.Token)")
    public void tokenPointCut() {
    }

    @Around("tokenPointCut()")
    public Object around(ProceedingJoinPoint joinPoint) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        Result result = JwtUtil.parseJWT(token);
        try {
            if (result.getCode() != 70001) {
                return joinPoint.proceed();
            } else {
                result.setResultCode(ResultCode.PERMISSION_NO_ACCESS);
            }
        } catch (Throwable throwable) {
            result.setResultCode(ResultCode.PERMISSION_NO_ACCESS);

        }
        result.setData(null);
        return result;
    }
}
